#makefile

CROSS_COMPILE =  
CC = $(CROSS_COMPILE)gcc
RANLIB = $(CROSS_COMPILE)ranlib
AR = $(CROSS_COMPILE)ar
CFLAGS := -g -O2 -D_REENTRANT -Wall

PROJ := proj
LIBS := -lpthread
OBJ_NAME := libmylib
SOURCE := $(wildcard *.c)
OBJS := $(patsubst %.c,%.o,$(SOURCE))

INSTALL_DIR := ../install/
OUT_DIR := ../obj/$(PROJ)/

ifeq ($(PROJ),LTE)
CROSS_COMPILE = arm-none-linux-gnueabi-
INSTALL_DIR := ~/exchange/LTE/tftpboot/
endif

ifeq ($(PROJ),WLAN)
CROSS_COMPILE = arm-none-linux-gnueabi-
INSTALL_DIR := ~/exchange/huiyan2.0/program/huiyan/lib/
TFTP_BOOT := ~/exchange/huiyan2.0/tftpboot/
endif

ifeq ($(PROJ),SKUO)
CROSS_COMPILE = arm-linux-uclibcgnueabi-
INSTALL_DIR := ~/exchange/skuo_programmer/program/lib/
TFTP_BOOT := ~/exchange/skuo_programmer/tftpboot/
endif
CLEANFILES := *.o *.so *.a


%.o:%.c mylib.h
	$(CC) -c -o $@ $< $(CFLAGS)


.PHONY:all
all: $(OUT_DIR)$(OBJ_NAME).so $(OUT_DIR)$(OBJ_NAME).a

$(OUT_DIR)$(OBJ_NAME).so:$(OBJS)
	@if [ -e $(OUT_DIR) ] ;\
	 then echo "$(OUT_DIR) exists - good." ;\
        else mkdir -p $(OUT_DIR);\
        fi;
	$(CC) -o $@ -fPIC -shared $^ $(LIBS)
	
$(OUT_DIR)$(OBJ_NAME).a:$(OBJS)
#	@if [ -e $(OUT_DIR) ] ;\
#	 then echo "$(OUT_DIR) exists - good." ;\
#      else mkdir -p $(OUT_DIR);\
#      fi;
	$(AR) rv $@ $?
	$(RANLIB) $@
	cp mylib.h $(OUT_DIR)

install:
	@if [ -e $(INSTALL_DIR) ] ;\
	 then echo "$(INSTALL_DIR) exists - good." ;\
        else mkdir -p $(INSTALL_DIR);\
        fi;	
	cd $(OUT_DIR);cp *.so *a $(INSTALL_DIR);cp *.so *a $(TFTP_BOOT)
	cp mylib.h $(INSTALL_DIR)
	
clean:
	rm -f $(CLEANFILES)
